package com.shinewind.payment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinewind.info.service.IModelCommentService;
import com.shinewind.info.service.IModelInfoService;
import com.shinewind.payment.mapper.ISuccessOrderMapper;
import com.shinewind.payment.service.IRelateGoodsService;
import com.shinewind.payment.service.ISuccessOrderService;
import com.shinewind.pojo.dto.ModelCommentDeleteDto;
import com.shinewind.pojo.dto.ModelCommentDto;
import com.shinewind.pojo.entity.ModelComment;
import com.shinewind.pojo.entity.ModelInfo;
import com.shinewind.pojo.entity.RelateGoods;
import com.shinewind.pojo.entity.SuccessOrder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SuccessOrderServiceImpl extends ServiceImpl<ISuccessOrderMapper, SuccessOrder> implements ISuccessOrderService {
    @DubboReference
    private IModelInfoService iModelInfoService;
    @DubboReference
    private IModelCommentService iModelCommentService;
    @Autowired
    private IRelateGoodsService iRelateGoodsService;

    @Override
    public Boolean saveSuccessOrderByUidANDOrderID(String uId, String orderId) {
        //将订单信息加入成功表
        SuccessOrder successOrder = SuccessOrder.builder().orderId(orderId).uId(uId).build();
        return this.save(successOrder) == true ? true : false;
    }

    @Override
    public Boolean saveCommentByDto(ModelCommentDto modelCommentDto) {
        //只有购买过该商品才允许该用户评论
        //根据传递的详情id查询该模型id
        ModelInfo modelInfo = iModelInfoService.getModelInfoBydId(modelCommentDto.getDId());
        //从关联商品表中查询买过该模型的用户
        List<RelateGoods> relateGoods = iRelateGoodsService.lambdaQuery().eq(RelateGoods::getMId,modelInfo.getMId()).list();
        //有人购买过此商品，遍历找出该用户
        if(relateGoods.size() > 0){
            for(RelateGoods relateGood:relateGoods){
                //根据订单号查询用户id
                if(this.lambdaQuery().eq(SuccessOrder::getOrderId,relateGood.getOrderId()).count() > 0){
                    SuccessOrder successOrder = this.lambdaQuery().eq(SuccessOrder::getOrderId,relateGood.getOrderId())
                            .one();
                    //该用户购买过商品
                    if(successOrder.getUId().equals(modelCommentDto.getUId())){
                        //该用户可以评论
                        ModelComment modelComment = ModelComment.builder().build();
                        BeanUtils.copyProperties(modelCommentDto,modelComment);
                        return iModelCommentService.save(modelComment) == true ? true : false;
                    }
                    //未知错误
                    else{
                        return false;
                    }
                }
            }
        }
        //买过此商品的人一个都没有
        else{
            return false;
        }
        return false;
    }

    @Override
    public Boolean deleteCommentByDto(ModelCommentDeleteDto modelCommentDeleteDto) {
        ModelComment modelComment = ModelComment.builder().build();
        BeanUtils.copyProperties(modelCommentDeleteDto,modelComment);
        return iModelCommentService.deleteCommentByDto(modelComment);
    }
}
